package com.lxyk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.mapper.SalaryMapper;
import com.lxyk.pojo.Salary;
import com.lxyk.pojo.Worker;
import com.lxyk.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SalaryServiceImpl
 * @Description TODO
 * @Author FJQ
 * @Date 2022/10/26 15:06
 * @Version 1.0
 **/
@Service
public class SalaryServiceImpl implements SalaryService {

    @Autowired
    private SalaryMapper salaryMapper;

   /* public PageInfo<Salary> calcSalaryByCondition(String startDate, String endDate,Integer pageNum,Integer pageSize) {
        //条件成立：表示“结束时间为空”，系统当前时间为结束时间，然后转换为字符串
        if (StrUtil.isEmpty(endDate)){
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            endDate = sdf.format(date);
        }

        //在某个时间段已经为客户送过水的送水工信息
        List<Salary> salaryList = salaryMapper.calcSalaryByCondition(startDate, endDate);

        //没有为客户送过水的送水工信息
        List<Worker> workerList = salaryMapper.queryNotSendWaterWorker();

        //获取送水的送水工名称
        List<String> workerNameList = salaryList.stream().map(Salary::getWorkerName).collect(Collectors.toList());
        //将没有送水的送水工信息合并到salaryList集合
        //遍历workerList  将worker对象注入到 Salary对象中，然后添加到 salaryList
        workerList.forEach(worker -> {
            if (!workerNameList.contains(worker.getWorkerName())){
                Salary sa = new Salary();
                sa.setWorkerName(worker.getWorkerName());
                sa.setWorkerSalary(worker.getWorkerSalary());
                sa.setWorkerMoney(worker.getWorkerMoney());
                //没有送水默认数量为0
                sa.setSendWaterCount(0);
                //没有送水实发工资为默认基本工资
                sa.setFinalSalary(Double.valueOf(worker.getWorkerSalary()));
                salaryList.add(sa);
            }
        });

        //工资排序  降序
        Collections.sort(salaryList,(o1,o2)->{
            if(o1.getFinalSalary() >o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary()  < o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });

        return new PageInfo<>(salaryList);
    }*/

    @Override
    public PageInfo<Salary> calcSalaryByCondition(String startDate, String endDate, Integer pageNum, Integer pageSize) {
        //条件成立：表示“结束时间为空”，系统当前时间为结束时间，然后转换为字符串
        if (StrUtil.isEmpty(endDate)){
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            endDate = sdf.format(date);
        }
        List<Salary> salaryList = salaryMapper.calcSalaryByCondition(startDate, endDate);
        //工资排序  降序
        Collections.sort(salaryList,(o1,o2)->{
            if(o1.getFinalSalary() >o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary()  < o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });
        return new PageInfo<>(salaryList);
    }

    @Override
    public PageInfo<Salary> calcSalary(Integer pageNum, Integer pageSize) {
        List<Salary> salaryList = salaryMapper.calcSalary();
        //工资排序  降序
        Collections.sort(salaryList,(o1,o2)->{
            if(o1.getFinalSalary() >o2.getFinalSalary()){
                return -1;
            }else if (o1.getFinalSalary()  < o2.getFinalSalary()){
                return 1;
            }else {
                return 0;
            }
        });
        return new PageInfo<>(salaryList);
    }

    @Override
    public PageInfo<Salary> IsNullWorker(Integer pageNum, Integer pageSize) {
        List<Salary> salaryList = salaryMapper.IsNullWorker();
        return new PageInfo<>(salaryList);
    }

}
